<!-- 基本信息 -->
<script setup lang="ts">
import { ref, computed, watch } from "vue"
import type { EditInfrenceBasicInfo } from "@/common/apis/train/type"
import { FormTypeEnum, taskModeMap } from "@/common/constants/other-keys"
interface Props {
  dialogType: FormTypeEnum
  ruleForm: EditInfrenceBasicInfo
  isEdit?: boolean
}
const props = defineProps<Props>()
const emit = defineEmits(["update:ruleForm"])
const formRef = ref()

// 创建本地状态以便正确验证
const localConfig = ref({
  is_enable: false,
  params: {
    ip: '',
    vhost: '',
    username: '',
    password: '',
    rmpAddress: '',
  }
})

// 监听props.ruleForm.situation_config的变化，同步到本地状态
watch(() => props.ruleForm.situation_config, (newValue) => {
  if (newValue) {
    localConfig.value.is_enable = newValue.is_enable
    localConfig.value.params.ip = newValue.params.ip || ''
    localConfig.value.params.vhost = newValue.params.vhost || ''
    localConfig.value.params.username = newValue.params.username || ''
    localConfig.value.params.password = newValue.params.password || ''
    localConfig.value.params.rmpAddress = newValue.params.rmpAddress || ''
  }
}, { immediate: true, deep: true })

// 监听本地状态的变化，同步回ruleForm
watch(localConfig, (newValue) => {
  if (!props.ruleForm.situation_config) {
    props.ruleForm.situation_config = {
      is_enable: newValue.is_enable,
      params: { ...newValue.params }
    }
  } else {
    props.ruleForm.situation_config.is_enable = newValue.is_enable
    props.ruleForm.situation_config.params = { ...newValue.params }
  }
}, { deep: true })

// 修改handleChange函数参数类型
const handleChange = (value: string | number | boolean | undefined) => {
  // 如果选择"否"，清空验证错误状态
  if (value === false && formRef.value) {
    formRef.value.clearValidate()
  }
}

// 表单校验规则
const rules = {
  ip: [
    { required: true, message: '请输入ip', trigger: 'change' },
  ],
  vhost: [
    { required: true, message: '请输入vhost', trigger: 'blur' },
  ],
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ]
};

const rmpAddress = computed(() => {
  if (localConfig.value.params.ip) {
    return "ws://" + localConfig.value.params.ip + ":15674/ws";
  }
  return "";
})

// 暴露验证方法给父组件
defineExpose({
  validate: async () => {
    // 如果没有态势信息配置，直接通过验证
    if (!props.ruleForm.situation_config) {
      return true
    }
    // 如果选择了不配置态势信息，直接通过验证
    if (!props.ruleForm.situation_config.is_enable) {
      return true
    }

    // 如果选择了配置态势信息，需要验证表单
    if (!formRef.value) return true

    try {
      await formRef.value.validate()
      return true
    } catch (error) {
      return false
    }
  }
})
</script>

<template>
  <div class="steps-wrapper">
    <div class="cpi-header-title">
      <SvgIcon name="info-title" />
      <span class="cpi-header-text">态势信息配置</span>
    </div>
    <div class="radio-group">
      <span class="radio-text">是否配置：</span>
      <el-radio-group @change="handleChange" class="radio-group" v-model="localConfig.is_enable"
        v-if="isEdit || dialogType == FormTypeEnum.CREATE">
        <el-radio :value="true">是</el-radio>
        <el-radio :value="false">否</el-radio>
      </el-radio-group>
      <span v-else>{{ localConfig.is_enable ? '是' : '否' }}</span>
    </div>
    <div v-if="localConfig.is_enable">
      <el-form ref="formRef" v-if="isEdit || dialogType == FormTypeEnum.CREATE" :model="localConfig.params" label-width="80"
        :rules="rules" label-position="right">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="ip" prop="ip">
              <el-input v-model="localConfig.params.ip" placeholder="请输入ip" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="vhost" prop="vhost">
              <el-input v-model="localConfig.params.vhost" placeholder="请输入vhost" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="localConfig.params.username" autocomplete="new-password" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="password">
              <el-input v-model="localConfig.params.password" autocomplete="new-password" placeholder="请输入"
                type="password" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="rmp地址">
              <el-input v-model="rmpAddress" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-form v-else :model="localConfig" label-width="80" label-position="right">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="ip：" prop="params.ip">
              {{ localConfig.params.ip }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="vhost：" prop="params.vhost">
              {{ localConfig.params.vhost }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用户名：" prop="params.username">
              {{ localConfig.params.username }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码：" prop="params.password">
              {{ localConfig.params.password }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="rmp地址：">
              {{ localConfig.params.rmpAddress }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>


  </div>
</template>
<style lang="scss" scoped>
.steps-wrapper {
  padding: 20px;
  width: 100%;
  height: auto;
  background: #031527;
  margin-top: 20px;
}

.radio-text {
  display: inline-block;
  margin-right: 10px;
  margin-left: 10px;
}

.radio-group {
  margin-bottom: 10px;
}

.no-config-message {
  color: #909399;
  font-size: 14px;
  padding: 10px 0;
}

.cpi-header-title {
  margin-bottom: 20px;
}

.cpi-header-text {
  margin-left: 6px;
}
</style>
